package cn.anecansaitin.whimsy.client.gui.overlay;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.capabilities.battle.DoomfistBattle;
import cn.anecansaitin.whimsy.common.capabilities.battle.IBattleMode;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.client.gui.overlay.ExtendedGui;
import net.neoforged.neoforge.client.gui.overlay.IGuiOverlay;

public class DoomfistOverlay implements IGuiOverlay {
    private static final ResourceLocation RESOURCE = Whimsy.resource("textures/gui/overlay/doomfist.png");

    @Override
    public void render(ExtendedGui gui, GuiGraphics guiGraphics, float partialTick, int screenWidth, int screenHeight) {
        Minecraft mc = Minecraft.getInstance();

        IBattleMode battleMode = mc.player.getCapability(IBattleMode.BATTLE_MODE);

        if (battleMode == null || !battleMode.isInMode()) {
            return;
        }

        if (!(battleMode.getController() instanceof DoomfistBattle doomFist)) {
            return;
        }
        {
            guiGraphics.drawCenteredString(gui.getFont(), doomFist.expandedHP.getTotalHP() + "", 40, 40, 0xFFFFFF);
        }
        //右下角技能图标的起始点
        int iconY = guiGraphics.guiHeight() - 50;
        int iconXStart = guiGraphics.guiWidth() - 135;

        //末日铁拳
        int midX = guiGraphics.guiWidth() / 2;
        int midY = guiGraphics.guiHeight() / 2;
        boolean rocketPunchCasting = doomFist.rocketPunch.isCasting();
        //技能图标
        //裂地重拳icon
        if (doomFist.seismicSlam.isCasting()) {
            guiGraphics.blit(RESOURCE, iconXStart, iconY, 0, 18, 18, 18);
        } else if (doomFist.seismicSlam.cd() <= 0) {
            guiGraphics.blit(RESOURCE, iconXStart, iconY, 0, 0, 18, 18);
        } else {
            int cd = (doomFist.seismicSlam.cd() + 20 - 1) / 20;//这是向上取整
            int i = doomFist.seismicSlam.cd() * 18 / doomFist.seismicSlam.defaultCd();
            guiGraphics.blit(RESOURCE, iconXStart, iconY + i, 0, 18 + i, 18, 18 - i);
            guiGraphics.blit(RESOURCE, iconXStart, iconY + i, 0, i, 18, 1);
            guiGraphics.blit(RESOURCE, iconXStart, iconY, 0, 2 * 18, 18, i);
            guiGraphics.drawCenteredString(gui.getFont(), (int) Math.ceil((float) cd / doomFist.tickScaling) + "", iconXStart + 10, iconY + 5, 0xffffff);
            RenderSystem.enableBlend();//文字渲染后会关闭混合，得手动打开
        }
        //悍将格挡icon
        if (doomFist.powerBlock.isCasting()) {
            guiGraphics.blit(RESOURCE, iconXStart + 18 + 5, iconY, 18, 18, 18, 18);
        } else if (doomFist.powerBlock.cd() <= 0) {
            guiGraphics.blit(RESOURCE, iconXStart + 18 + 5, iconY, 18, 0, 18, 18);
        } else {
            int cd = (doomFist.powerBlock.cd() + 20 - 1) / 20;
            int i = doomFist.powerBlock.cd() * 18 / doomFist.powerBlock.defaultCd();
            guiGraphics.blit(RESOURCE, iconXStart + 18 + 5, iconY + i, 18, 18 + i, 18, 18 - i);
            guiGraphics.blit(RESOURCE, iconXStart + 18 + 5, iconY + i, 18, i, 18, 1);
            guiGraphics.blit(RESOURCE, iconXStart + 18 + 5, iconY, 18, 2 * 18, 18, i);
            guiGraphics.drawCenteredString(gui.getFont(), (int) Math.ceil((float) cd / doomFist.tickScaling) + "", iconXStart + 18 + 5 + 10, iconY + 5, 0xffffff);
            RenderSystem.enableBlend();
        }
        //火箭重拳icon
        if (doomFist.rocketPunch.isCasting()) {
            guiGraphics.blit(RESOURCE, iconXStart + 2 * (18 + 5), iconY, 36, 18, 18, 18);
        } else if (doomFist.rocketPunch.cd() <= 0) {
            guiGraphics.blit(RESOURCE, iconXStart + 2 * (18 + 5), iconY, 36, 0, 18, 18);
        } else {
            int cd = (doomFist.rocketPunch.cd() + 20 - 1) / 20;
            int i = doomFist.rocketPunch.cd() * 18 / doomFist.rocketPunch.defaultTick();
            guiGraphics.blit(RESOURCE, iconXStart + 2 * (18 + 5), iconY + i, 36, 18 + i, 18, 18 - i);
            guiGraphics.blit(RESOURCE, iconXStart + 2 * (18 + 5), iconY + i, 36, i, 18, 1);
            guiGraphics.blit(RESOURCE, iconXStart + 2 * (18 + 5), iconY, 36, 2 * 18, 18, i);
            guiGraphics.drawCenteredString(gui.getFont(), (int) Math.ceil((float) cd / doomFist.tickScaling) + "", iconXStart + 2 * (18 + 5) + 10, iconY + 5, 0xffffff);
            RenderSystem.enableBlend();
        }

        //火箭重拳蓄力
        if (rocketPunchCasting && doomFist.rocketPunch.isCharging()) {
            int y = midY + 20;
            int boxHeight = 5;
            int rechargeTick = (int) Math.ceil((float) doomFist.rocketPunch.rechargeTick() / doomFist.tickScaling);

            if (rechargeTick >= (int) Math.ceil((float) doomFist.rocketPunch.maxRechargeTick()) / doomFist.tickScaling) {
                guiGraphics.fill(midX - 13, y + boxHeight, midX + 13, y, 0xffd1e8ff);
                guiGraphics.fill(midX - 18, y + 2 * boxHeight + 1, midX + 18, y + boxHeight + 1, 0xffd1e8ff);
                guiGraphics.fill(midX - 22, y + 3 * boxHeight + 2, midX + 22, y + 2 * boxHeight + 2, 0xffd1e8ff);
                guiGraphics.fill(midX - 26, y + 4 * boxHeight + 3, midX + 26, y + 3 * boxHeight + 3, 0xffd1e8ff);
            } else if (rechargeTick > 15) {
                int y1 = y + boxHeight - (rechargeTick - 15);
                guiGraphics.fill(midX - 13, y + boxHeight, midX + 13, y1, 0x7f00d8ff);
                guiGraphics.fill(midX - 13, y1, midX + 13, y, 0x3fbaf5ff);
                guiGraphics.fill(midX - 18, y + 2 * boxHeight + 1, midX + 18, y + boxHeight + 1, 0x7f00d8ff);
                guiGraphics.fill(midX - 22, y + 3 * boxHeight + 2, midX + 22, y + 2 * boxHeight + 2, 0x7f00d8ff);
                guiGraphics.fill(midX - 26, y + 4 * boxHeight + 3, midX + 26, y + 3 * boxHeight + 3, 0x7f00d8ff);
            } else if (rechargeTick > 10) {
                guiGraphics.fill(midX - 13, y + boxHeight, midX + 13, y, 0x3fbaf5ff);
                int y1 = y + 2 * boxHeight + 1 - (rechargeTick - 10);
                guiGraphics.fill(midX - 18, y + 2 * boxHeight + 1, midX + 18, y1, 0x7f00d8ff);
                guiGraphics.fill(midX - 18, y1, midX + 18, y + boxHeight + 1, 0x3fbaf5ff);
                guiGraphics.fill(midX - 22, y + 3 * boxHeight + 2, midX + 22, y + 2 * boxHeight + 2, 0x7f00d8ff);
                guiGraphics.fill(midX - 26, y + 4 * boxHeight + 3, midX + 26, y + 3 * boxHeight + 3, 0x7f00d8ff);
            } else if (rechargeTick > 5) {
                guiGraphics.fill(midX - 13, y + boxHeight, midX + 13, y, 0x3fbaf5ff);
                guiGraphics.fill(midX - 18, y + 2 * boxHeight + 1, midX + 18, y + boxHeight + 1, 0x3fbaf5ff);
                int y1 = y + 3 * boxHeight + 2 - (rechargeTick - 5);
                guiGraphics.fill(midX - 22, y + 3 * boxHeight + 2, midX + 22, y1, 0x7f00d8ff);
                guiGraphics.fill(midX - 22, y1, midX + 22, y + 2 * boxHeight + 2, 0x3fbaf5ff);
                guiGraphics.fill(midX - 26, y + 4 * boxHeight + 3, midX + 26, y + 3 * boxHeight + 3, 0x7f00d8ff);
            } else if (rechargeTick > 0) {
                guiGraphics.fill(midX - 13, y + boxHeight, midX + 13, y, 0x3fbaf5ff);
                guiGraphics.fill(midX - 18, y + 2 * boxHeight + 1, midX + 18, y + boxHeight + 1, 0x3fbaf5ff);
                guiGraphics.fill(midX - 22, y + 3 * boxHeight + 2, midX + 22, y + 2 * boxHeight + 2, 0x3fbaf5ff);
                int y1 = y + 4 * boxHeight + 3 - rechargeTick;
                guiGraphics.fill(midX - 26, y + 4 * boxHeight + 3, midX + 26, y1, 0x7f00d8ff);
                guiGraphics.fill(midX - 26, y1, midX + 26, y + 3 * boxHeight + 3, 0x3fbaf5ff);
            }
        } else if (doomFist.powerBlock.isCasting()) {//格挡指示条
            int y = midY - 32;
            int x = midX + 42;
            int boxWidth = 5;
            int boxHeight = 64;

            if (doomFist.powerBlock.blockedDamage() >= doomFist.powerBlock.maxDamage()) {
                guiGraphics.fill(x, y, x + boxWidth, y + boxHeight, 0xffffffff);
            } else {
                int v = (int) (doomFist.powerBlock.blockedDamage() * boxHeight / doomFist.powerBlock.maxDamage());
                guiGraphics.fill(x, y, x + boxWidth, y + boxHeight - v, 0x3fffffff);
                guiGraphics.fill(x, y + boxHeight - v, x + boxWidth, y + boxHeight, 0x7fffffff);
            }
        }

        //手炮
        int count = doomFist.handCannon.bulletCount();

        //准星下方的弹药指示
        if (!rocketPunchCasting) {
            int y = midY + 30;
            int boxWidth = 13;
            int boxHeight = 4;
            guiGraphics.fill(midX - 3 * boxWidth - 18, y + boxHeight, midX - 3 * boxWidth - 16, y + boxHeight + 2, 0x3fffffff);
            guiGraphics.fill(midX - 3 * boxWidth - 13, y + boxHeight + 1, midX - 2 * boxWidth - 10, y + boxHeight + 2, 0x3fffffff);
            guiGraphics.fill(midX - 2 * boxWidth - 6, y + boxHeight, midX - boxWidth - 6, y + boxHeight * 2, count >= 4 ? 0x7f00d8ff : 0x3fbaf5ff);
            guiGraphics.fill(midX - boxWidth - 2, y, midX - 2, y + boxHeight, count >= 3 ? 0x7f00d8ff : 0x3fbaf5ff);
            guiGraphics.fill(midX + 2, y, midX + boxWidth + 2, y + boxHeight, count >= 2 ? 0x7f00d8ff : 0x3fbaf5ff);
            guiGraphics.fill(midX + boxWidth + 6, y + boxHeight, midX + 2 * boxWidth + 6, y + boxHeight * 2, count >= 1 ? 0x7f00d8ff : 0x3fbaf5ff);
            guiGraphics.fill(midX + 2 * boxWidth + 10, y + boxHeight + 1, midX + 3 * boxWidth + 13, y + boxHeight + 2, 0x5fffffff);
            guiGraphics.fill(midX + 3 * boxWidth + 16, y + boxHeight, midX + 3 * boxWidth + 18, y + boxHeight + 2, 0x3fffffff);
        }
        //右下角弹药
        guiGraphics.blit(RESOURCE, iconXStart + 3 * (18 + 5), iconY, 54, 0, 46, 18);
        //上横线
        guiGraphics.fill(iconXStart + 3 * (18 + 5), iconY, iconXStart + 5 * (18 + 5), iconY + 1, 0x7f00d8ff);
        //上横线左侧点
        guiGraphics.fill(iconXStart + 3 * (18 + 5), iconY + 1, iconXStart + 3 * (18 + 5) + 1, iconY + 2, 0x7f00d8ff);
        //上横线右侧点
        guiGraphics.fill(iconXStart + 5 * (18 + 5) - 1, iconY + 1, iconXStart + 5 * (18 + 5), iconY + 2, 0x7f00d8ff);
        //下横线
        guiGraphics.fill(iconXStart + 3 * (18 + 5), iconY + 17, iconXStart + 5 * (18 + 5), iconY + 18, 0x7f00d8ff);
        //下横线左侧点
        guiGraphics.fill(iconXStart + 3 * (18 + 5), iconY + 17 - 1, iconXStart + 3 * (18 + 5) + 1, iconY + 18 - 1, 0x7f00d8ff);
        //下横线右侧点
        guiGraphics.fill(iconXStart + 5 * (18 + 5) - 1, iconY + 17 - 1, iconXStart + 5 * (18 + 5), iconY + 18 - 1, 0x7f00d8ff);
        //子弹数间隔线
        guiGraphics.fill(iconXStart + 3 * (18 + 5) + 34, iconY + 4, iconXStart + 3 * (18 + 5) + 35, iconY + 14, 0x7f00d8ff);
        guiGraphics.drawCenteredString(gui.getFont(), count + " " + 4, iconXStart + 3 * (18 + 5) + 35, iconY + 6, 0xffffff);
    }
}
